﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace demo_doan
{
    public partial class frmLapTheDocGia : Form
    {
        frmMain myParent;
        public frmLapTheDocGia(frmMain _myParent)
        {
            myParent = _myParent;
            InitializeComponent();
        }
        //public frmLapTheDocGia()
        //{
        //    InitializeComponent();
        //}

        private void frmLapTheDocGia_Load(object sender, EventArgs e)
        {
            if (myParent.isLogin)
            {
                llbTenNV.Visible = true;
                llbThoat.Visible = true;
                llbTenNV.Text = frmMain.GetObj().all.HoTen;
                llbTenNV.Left = llbThoat.Left - (llbTenNV.Size.Width + 3); 
            }
            List<DTO.LoaiDocGia> dsLoaiDG = BUS.LoaiDocGia.DanhSachLoaiDocGia();
            cbLoaiDocGia.DataSource = dsLoaiDG;
            cbLoaiDocGia.DisplayMember = "TenLoaiDocGia";
            cbLoaiDocGia.ValueMember = "MaLoaiDocGia";

            List<DTO.NhanVien> dsNV = new List<DTO.NhanVien>();
            if (BUS.NhanVien.SelectTT(dsNV) == BUS.NhanVienBUSResult.OK)
            {
                cbNhanVienLapThe.DataSource = dsNV;
                cbNhanVienLapThe.DisplayMember = "HoTen";
                cbNhanVienLapThe.ValueMember = "MaNhanVien";
            }
        }
        static public bool KiemTraEmail(string email)
        {


            if (email.IndexOf(" ") > 0)
            {
                return false;
            }
            if (email.IndexOf("@") == -1)
            {
                return false;
            }
            if (email.IndexOf(".") == -1)
            {
                return false;
            }
            if (email.IndexOf("..") != -1)
            {
                return false;
            }
            if (email.IndexOf("@") != email.LastIndexOf("@"))
            {
                return false;
            }
            if (email.LastIndexOf(".") == email.Length - 1)
            {
                return false;
            }

            string str = "abcdefghikjlmnopqrstuvwxyz-@._0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            for (int i = 0; i < email.Length; i++)
            {
                if (str.IndexOf(email[i]) == -1)
                {
                    return false;
                }

            }


            return true;

        }

        public bool kiemtra()
        {
            if (txtHoTen.Text == "")
            {
                MessageBox.Show("Nhap ho ten !!!", "Thông Báo");
                return false;
            }
            if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Nhap dia chi !!!", "Thông Báo");
                return false;
            }
            if (txtEmail.Text == "")
            {
                MessageBox.Show("Nhap Email !!!", "Thông Báo");
                return false;
            }
            if (!KiemTraEmail(txtEmail.Text))
            {
                MessageBox.Show("Nhap dung kieu Email !!!", "Thông Báo");
                return false;
            }
            return true;
        }

        private void btnLapThe_Click(object sender, EventArgs e)
        {
            //List<DTO.DocGia> dg = BUS.DocGia.
            if (!kiemtra())
                return;
            DTO.DocGia dg = new DTO.DocGia();
            if (BUS.DocGia.PhatSinhMaDocGia(dg) == BUS.DocGiaBUSResult.OK)
            {
              
                dg.HoTen = txtHoTen.Text;
                dg.DiaChi = txtDiaChi.Text;
                dg.Email = txtEmail.Text;
                dg.LoaiDocGia =int.Parse( cbLoaiDocGia.SelectedValue.ToString());
                dg.MaNhanVien = cbNhanVienLapThe.SelectedValue.ToString();
                dg.NgaySinh = dtpNgaySinh.Value;
                dg.NgayLapThe = dtpNgayLap.Value;
                dg.NgayHetHan = dg.NgayLapThe.AddDays(30);
                if (BUS.DocGia.Insert(dg) == BUS.DocGiaBUSResult.OK)
                {
                    MessageBox.Show("Thêm Thành Công", "Thông Báo");
                }
            }
            else
                MessageBox.Show("Lỗi", "Thông Báo");
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            cbLoaiDocGia.DataSource = null;
            cbNhanVienLapThe.DataSource = null;
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            frmLapTheDocGia_Load(sender, e);
        }

        private void llbThoat_LinkClicked(object sender , LinkLabelLinkClickedEventArgs e)
        {
            llbThoat.Visible = false;
            llbTenNV.Visible = false;
            myParent.isLogin = false;
            myParent.menuDangNhapVisible = true;
            myParent.frmMain_Load(sender , e);  
        }

        private void llbTenNV_LinkClicked(object sender , LinkLabelLinkClickedEventArgs e)
        {
            myParent.mainPanel.Controls.Clear();
            Form temp = new frmThayDoiThongTin(myParent);
            temp.MdiParent = myParent;
            myParent.mainPanel.Controls.Add(temp);
            temp.Show();
            myParent.Size = new Size(200 , 200);
        }
    }
}